using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using RealState.BusinessObjects;

namespace RealState.DAL
{
	public class DistrictsDAO
	{
		public DistrictsDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Districts> GetDistrictss()
		{
			try
			{
				List<Districts> lstDistrictss = new List<Districts>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTDistrictss",CommandType.StoredProcedure);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Districts oDistricts = new Districts();
					oDistricts.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oDistricts.DistrictName = Convert.ToString(oDbDataReader["DistrictName"]);
					oDistricts.CityID = Convert.ToInt32(oDbDataReader["CityID"]);

					if(oDbDataReader["IsPublish"] != DBNull.Value)
						oDistricts.IsPublish = Convert.ToInt32(oDbDataReader["IsPublish"]);

					if(oDbDataReader["Sorted"] != DBNull.Value)
						oDistricts.Sorted = Convert.ToInt32(oDbDataReader["Sorted"]);
					lstDistrictss.Add(oDistricts);
				}
				oDbDataReader.Close();
				return lstDistrictss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Districts GetDistricts(int Id)
		{
			try
			{
				Districts oDistricts = new Districts();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTDistricts",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oDistricts.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oDistricts.DistrictName = Convert.ToString(oDbDataReader["DistrictName"]);
					oDistricts.CityID = Convert.ToInt32(oDbDataReader["CityID"]);

					if(oDbDataReader["IsPublish"] != DBNull.Value)
						oDistricts.IsPublish = Convert.ToInt32(oDbDataReader["IsPublish"]);

					if(oDbDataReader["Sorted"] != DBNull.Value)
						oDistricts.Sorted = Convert.ToInt32(oDbDataReader["Sorted"]);
				}
				oDbDataReader.Close();
				return oDistricts;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddDistricts(Districts oDistricts)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERTDistricts",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictName",DbType.String,oDistricts.DistrictName));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityID",DbType.Int32,oDistricts.CityID));
				if (oDistricts.IsPublish.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsPublish",DbType.Int32,oDistricts.IsPublish));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsPublish",DbType.Int32,DBNull.Value));
				if (oDistricts.Sorted.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Sorted",DbType.Int32,oDistricts.Sorted));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Sorted",DbType.Int32,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateDistricts(Districts oDistricts)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEDistricts",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictName",DbType.String,oDistricts.DistrictName));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityID",DbType.Int32,oDistricts.CityID));
				if (oDistricts.IsPublish.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsPublish",DbType.Int32,oDistricts.IsPublish));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsPublish",DbType.Int32,DBNull.Value));
				if (oDistricts.Sorted.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Sorted",DbType.Int32,oDistricts.Sorted));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Sorted",DbType.Int32,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oDistricts.Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveDistricts(Districts oDistricts)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEDistricts",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oDistricts.Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveDistricts(int Id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEDistricts",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
